/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "CoNumberEditor.h"
#include "onyx/ui/number_dialog.h"

/**
 * Constructor. 
 * 
 * @author reach (5/25/2010)
 * 
 * @param text: Text for the label.
 * @param unit: Unit of data.
 * @param max: Maximal value.
 * @param rate: The rate of the ture value and the OnyxNumberDialog input value.
 * @param parent: Parent widget.
 */
CoNumberEditor::CoNumberEditor(const QString& text, const QString& unit, float max, float rate, QWidget* parent) :
QWidget(parent),
m_label(text, this),
m_editor("", this),
m_layout(this),
m_unit(unit),
m_rate(rate),
m_max(max)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    setLayout(&m_layout);
    m_layout.addWidget(&m_label);
    m_layout.addWidget(&m_editor);

    connect(&m_editor, SIGNAL(clicked()), this, SLOT(inputNumber()));

    setValue(0);

    Out_Log(__FUNCTION__, COCLASSNAME);
}

/**
 * Set the value of this number editor.
 * 
 * @author reach (5/25/2010)
 * 
 * @param value 
 */
void CoNumberEditor::setValue(float value)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_value = value;
    m_editor.setText(QString::number(value) + m_unit);

    Out_Log(__FUNCTION__, COCLASSNAME);
}

/**
 * Get the value of this number editor. 
 * 
 * @author reach (5/25/2010)
 * 
 * @return float: 
 */
float CoNumberEditor::getValue()
{
    return m_value;
}

/**
 * Popup the OnyxNumberDialog to receive users' input.
 * 
 * @author reach (5/25/2010)
 */
void CoNumberEditor::inputNumber()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    ui::NumberDialog dlg(this);
    dlg.updateTitle(tr("输入数字"));
    int ret = dlg.popup(0, static_cast<int>(m_max/m_rate));
    emit updateRequest();

    if (ret == QDialog::Rejected)
    {
        Out_Log(__FUNCTION__, COCLASSNAME);
        return; 
    }

    setValue(dlg.value() * m_rate);

    Out_Log(__FUNCTION__, COCLASSNAME);
}
